﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Exchange.WebServices.Auth;
using System.Net;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using VBModel;

namespace VBBLL
{
    public static class Worker 
    {
        public static List<string> getEmailAddress()
        {
            string cString = WebConfigurationManager.ConnectionStrings["WorkFlowConnectionString"].ConnectionString;
            string strSQL = "SELECT [Mail] FROM [WorkFlow].[dbo].[RUC_Users] where IfDelete=0 and Mail like'%@kaifa%' order by Mail ";
            DataTable dt = GetDataSet(strSQL, cString).Tables[0];
            return dt.AsEnumerable().Select(p => p[0].ToString().ToLower()).ToList();
        }

        public static void writeErrorMessage(string meetingroomId, Exception ex, string userName)
        {
            try
            {
                string errorInnerMessage;
                if (ex.InnerException != null)
                {
                    errorInnerMessage = ex.InnerException.ToString().Replace("'", "\"");
                }
                else
                {
                    errorInnerMessage = "";
                }
                string errorMessage = ex.Message.ToString().Replace("'", "\"");
                if (errorInnerMessage.Length > 1000)
                {
                    errorInnerMessage = errorInnerMessage.Substring(0, 990);
                }
                if (errorMessage.Length > 500)
                {
                    errorMessage = errorMessage.Substring(0, 490);
                }
                MTR_bas_ErrorMessage err = new MTR_bas_ErrorMessage();
                err.meetingroomId = meetingroomId;
                err.errorInnerMessage = errorInnerMessage;
                err.errorMessage = errorMessage;
                err.userName = userName;
                err.errorTime = DateTime.Now;
                new BaseRepositoryBLL<MTR_bas_ErrorMessage>().AddEntities(err);
            }
            catch (Exception e)
            {
                writeErrorMessage("", e, "writeErrorMessage");
            }
        }

        public static string[] getNameAndEmail()
        {
            string cString = WebConfigurationManager.ConnectionStrings["WorkFlowConnectionString"].ConnectionString;
            string strSQL = @"SELECT [GivenName],[SN],[Description],[Title] ,[Orge2TXT],[Orge3TXT],[Mail],IfDelete FROM [WorkFlow].[dbo].[RUC_Users] 
                        where  (IfDelete is null or IfDelete=0)and Mail like '%@kaifa%' ";
            DataTable dt = GetDataSet(strSQL, cString).Tables[0];
            string[] NameAndEmail = new string[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string item = (dt.Rows[i]["GivenName"].ToString() + dt.Rows[i]["SN"].ToString()).ToLower() + " ,"
                    + dt.Rows[i]["Description"].ToString() + " ,"
                    + dt.Rows[i]["Title"].ToString() + " ,"
                    + dt.Rows[i]["Orge2TXT"].ToString() + " ,"
                    + dt.Rows[i]["Orge3TXT"].ToString() + " ,"
                    + dt.Rows[i]["Mail"].ToString();
                item = item.Replace(", ,", ",");
                NameAndEmail[i] = item;
            }
            return NameAndEmail;
        }


        public static void doSql(string strSql, string strCon)
        {
            try
            {
                using (SqlConnection sqlConn = new SqlConnection(strCon))
                {
                    sqlConn.Open();
                    SqlCommand SCom = new SqlCommand();
                    SCom.CommandText = strSql;
                    SCom.Connection = sqlConn;
                    SCom.CommandType = CommandType.Text;
                    int num = SCom.ExecuteNonQuery();
                    sqlConn.Close();
                }
            }
            catch (Exception ex)
            {
                writeErrorMessage("0", ex, "DOSQL");
            }
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="strSql">sql语句</param>
        /// <param name="strCon">连接字符串</param>
        /// <returns></returns>
        public static DataSet GetDataSet(string strSql, string strCon)
        {
            try
            {
                using (SqlConnection sqlCnn = new SqlConnection(strCon))
                {
                    sqlCnn.Open();
                    SqlDataAdapter dataAdapter = new SqlDataAdapter(strSql, sqlCnn);
                    DataSet dataSet = new DataSet();
                    dataAdapter.SelectCommand.CommandTimeout = 480;
                    dataAdapter.Fill(dataSet);
                    sqlCnn.Close();
                    return dataSet;
                }
            }
            catch (Exception ex)
            {
                Worker.writeErrorMessage("0", ex, "GetDataSet");
                return null;
            }
        }
      
    }
}